package com.gy.bearhelps.setting;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gy.bearhelps.R;
import com.gy.bearhelps.base.BaseActivity;
import com.gy.bearhelps.setting.adapter.FileAdapter;
import com.gy.bearhelps.setting.bean.DownFileBean;
import com.gy.bearhelps.util.AndroidFileUtil;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 16/11/5
 * 说明：
 */
public class DownFileActivity extends BaseActivity {

    private ListView listView;
    private LinearLayout llSelect,llSelectAll,llEmpty;
    private ImageView imgSelectAll;
    private TextView tvDelete;
    private FileAdapter adapter;
    private List<DownFileBean> mLists;
    private List<Integer> selects;//选中的条目
    private int isEdit;//是否编辑状态 1是
    private int isSelectAll;//是否为全选状态 1是
    private File downFile;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_down_file);
        setTitleMsg("最近文件");
        _baseRight_text.setText("编辑");
        listView = (ListView) findViewById(R.id.activity_file_listview);
        llEmpty = (LinearLayout) findViewById(R.id.activity_file_empty);
        llSelect = (LinearLayout) findViewById(R.id.activity_file_ll_select);
        llSelectAll = (LinearLayout) findViewById(R.id.activity_file_ll_all);
        imgSelectAll = (ImageView) findViewById(R.id.activity_file_img_all);
        tvDelete = (TextView) findViewById(R.id.activity_file_tv_delete);
    }

    @Override
    protected void initData() {
        mLists = new ArrayList<>();
        selects = new ArrayList<>();
        downFile = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "bearhelp");
        File[] files = downFile.listFiles();
        for (File file : files){
            DownFileBean bean = new DownFileBean();
            bean.name = file.getName();
            bean.path = file.getAbsolutePath();
            mLists.add(bean);
        }
        adapter = new FileAdapter(this, mLists);
    }

    @Override
    protected void setListener() {
        super.setListener();
        llSelectAll.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(isEdit == 1){
                    if(selects.contains(i)){
                        selects.remove(Integer.valueOf(i));
                    } else {
                        selects.add(Integer.valueOf(i));
                    }
                    adapter.setSelects(selects);
                    if(selects.size() == mLists.size()){
                        imgSelectAll.setImageResource(R.mipmap.ic_check_select);
                    } else {
                        imgSelectAll.setImageResource(R.mipmap.ic_check_normal);
                    }
                } else {
                    Intent intent = AndroidFileUtil.openFile(mLists.get(i).path);
                    if(intent != null){
                        startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        if(mLists.size() > 0){
            llEmpty.setVisibility(View.GONE);
        } else {
            llEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id._right:
                if(isEdit == 0){
                    if(mLists.size() > 0){
                        isEdit = 1;
                        _baseRight_text.setText("取消");
                        llSelect.setVisibility(View.VISIBLE);
                    }
                } else {
                    isEdit = 0;
                    _baseRight_text.setText("编辑");
                    llSelect.setVisibility(View.GONE);
                }
                adapter.setIsEdit(isEdit);
                break;
            case R.id.activity_file_ll_all:
                if(isSelectAll == 0){
                    //全选
                    isSelectAll = 1;
                    imgSelectAll.setImageResource(R.mipmap.ic_check_select);
                    for (int i = 0; i < mLists.size(); i++) {
                        selects.add(i);
                    }
                    adapter.setSelects(selects);
                } else if(isSelectAll == 1){
                    //取消全选
                    isSelectAll = 0;
                    selects.clear();
                    imgSelectAll.setImageResource(R.mipmap.ic_check_normal);
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.activity_file_tv_delete:
                if(selects.size() == 0){
                    showToast("请选择要删除的文件");
                } else {
                    for (int i = mLists.size() - 1; i  >= 0; i--) {
                        if(selects.contains(i)){
                            File file = new File(mLists.get(i).path);
                            file.delete();
                            mLists.remove(i);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    if(mLists.size() == 0){
                        llSelect.setVisibility(View.GONE);
                        llEmpty.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
    }
}
